<style scoped>
    .corporate-factory-page{}

</style>
<template>
    <div class="corporate-factory-page">
        <el-form :inline="true" :model="formInline" class="demo-form-inline" style="border-bottom:1px solid #ccc;">
            <!--法人名称-->
            <el-form-item :label="nowLangue.guangyi2" class="selectMB10">
                <el-select size="mini" clearable  v-model="formInline.userId" :placeholder="nowLangue.pleaseSelect">
                    <el-option v-for="(legalPerson,index) in legalPersonDropDown" :key="index" :label="legalPerson.userName" :value="legalPerson.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <!--工厂编码-->
            <el-form-item :label="nowLangue.farenbianma" class="selectMB10">
                <el-input size="mini" v-model="formInline.legalPersonCode" :placeholder="nowLangue.pleaseImport+nowLangue.farenbianma"></el-input>
            </el-form-item>
            <!--工厂描述-->
            <el-form-item :label="nowLangue.farenmingcheng" class="selectMB10">
                <el-input size="mini" v-model="formInline.legalPersonName" :placeholder="nowLangue.pleaseImport+nowLangue.farenmingcheng"></el-input>
            </el-form-item>
            <!--当前状态-->
            <el-form-item :label="nowLangue.currStatus" class="selectMB10">
                <el-select size="mini" clearable v-model="formInline.enabled" :placeholder="nowLangue.pleaseSelect" style="width:90px;" >
                    <!--启用-->
                    <el-option :label="nowLangue.enable" value="true"></el-option>
                    <!--禁用-->
                    <el-option :label="nowLangue.forbidden" value="false"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="selectMB10">
                <!--查询-->
                <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="queryCorFactSheet()" v-text="nowLangue.query"></el-button>
            </el-form-item>
        </el-form>
        <!--表格-->
        <!--法人所属工厂明细列表：-->
        <label class="default-label" v-text="nowLangue.legalFactoryList"></label>
        <!--新增按钮-->
        <!--新增-->
        <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="createNew()" v-text="nowLangue.create"></el-button>
        <el-table :data="corFactoryData" border style="width: 100%;" v-loading.body="loading" element-loading-text="拼命加载中">
            <!--序-->
            <el-table-column type="index" header-align="center" align="center" :label="nowLangue.sequence"  width="40"></el-table-column>
            <!--操作-->
            <el-table-column :label="nowLangue.operate" header-align="center" align="center" width="80">
                <template scope="scope">
                    <!--禁用-->
                    <button class="operationItem bolder" v-if="scope.row.enabled == '启用' " @click="handleStatus(scope.$index,scope.row)" v-text="nowLangue.forbidden"></button>
                    <!--启用-->
                    <button class="operationItem bolder" v-else @click="handleStatus(scope.$index,scope.row)" v-text="nowLangue.enable"></button>
                    <!--修改-->
                    <button class="operationItem disabled bolder" v-if="scope.row.enabled == '启用'" v-text="nowLangue.update"></button>
                    <!--修改-->
                    <button class="operationItem bolder" v-else @click="updateCorFact(scope.$index,scope.row)" v-text="nowLangue.update"></button>
                </template>
            </el-table-column>
            <!--工厂编码-->
            <el-table-column prop="legalPersonCode" :label="nowLangue.farenbianma" header-align="center" width="100"  show-overflow-tooltip></el-table-column>
            <!--工厂描述-->
            <el-table-column prop="legalPersonName" :label="nowLangue.farenmincheng" header-align="center" show-overflow-tooltip></el-table-column>
            <!--当前状态-->
            <el-table-column prop="enabled" :label="nowLangue.currStatus" header-align="center" align="center"  width="80" show-overflow-tooltip></el-table-column>
            <!--法人编码-->
            <el-table-column prop="userName" :label="nowLangue.xiaolong" header-align="center"  width="100" show-overflow-tooltip></el-table-column>
            <!--法人名称-->
            <el-table-column prop="userId" :label="nowLangue.yonghuid" header-align="center" show-overflow-tooltip></el-table-column>

        </el-table>
        <!--分页-->
        <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>
        <!--新建弹窗-->
        <!--新增工厂信息-->
        <el-dialog :title="nowLangue.create+nowLangue.factoryMes" :visible.sync="dialogForm1Visible" size="tiny" :lock-scroll="false">
            <el-form :inline="true" :model="addCorFactData" class="demo-form-inline label-minW80" style="border-bottom:1px solid #ccc;" ref="addCorFactData">
                <!--用户名称-->
                <el-form-item :label="nowLangue.xiaolong" prop="userId"  :rules="[{ required: true, message: nowLangue.required}]" >
                    <el-select size="mini" clearable  v-model="addCorFactData.userId" :placeholder="nowLangue.pleaseSelect">
                        <el-option v-for="(legalPerson,index) in legalPersonDropDown" :key="index" :label="legalPerson.userName" :value="legalPerson.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <!--用户编码-->
                <el-form-item :label="nowLangue.yonghuid" class="selectMB10" prop="legalPersonCode" >
                    <el-input  v-model="addCorFactData.userId" disabled></el-input>
                </el-form-item>
                <!--工厂编码-->
                <el-form-item :label="nowLangue.farenbianma" prop="legalPersonName"  :rules="[{ required: true, message: nowLangue.required}]" >
                    <el-input  v-model.trim="addCorFactData.legalPersonName" :placeholder="nowLangue.pleaseImport+nowLangue.factoryCode"></el-input>
                </el-form-item>
                <!--工厂描述-->
                <el-form-item :label="nowLangue.farenmingcheng" prop="legalPersonCode"  :rules="[{ required: true, message: nowLangue.required}]" >
                    <el-input  v-model.trim="addCorFactData.legalPersonCode" :placeholder="nowLangue.pleaseImport+nowLangue.factoryDescription"></el-input>
                </el-form-item>
                <!--状态-->
                <el-form-item :label="nowLangue.status" class="selectMB10" >
                    <el-radio-group v-model="addCorFactData.enabled">
                        <!-- 启用 -->
                        <el-radio :label="true" v-cloak>{{nowLangue.enable}}</el-radio>
                        <!-- 禁用 -->
                        <el-radio :label="false" v-cloak>{{nowLangue.forbidden}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <!--保存-->
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="saveCorFact" v-text="nowLangue.save" :disabled="saveCFDisabled"></el-button>
                <!--取消-->
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="cancleCorFact" v-text="nowLangue.cancel"></el-button>
            </div>
        </el-dialog>
        <!--修改弹窗-->
        <!--修改工厂信息-->
        <el-dialog :title="nowLangue.update+nowLangue.factoryMes" :visible.sync="dialogForm2Visible" size="tiny" :lock-scroll="false">
            <el-form :inline="true" :model="updateCorFactData" class="demo-form-inline label-minW80" style="border-bottom:1px solid #ccc;" ref="updateCorFactData">
                <!--法人名称-->
                <el-form-item :label="nowLangue.xiaolong" class="selectMB10">
                    <el-input  v-model="updateCorFactData.userName" disabled></el-input>
                </el-form-item>
                <!--法人编码-->
                <el-form-item :label="nowLangue.yonghuid" class="selectMB10">
                    <el-input  v-model="updateCorFactData.userId" disabled></el-input>
                </el-form-item>
                <!--工厂编码-->
                <el-form-item :label="nowLangue.farenbianma" class="selectMB10">
                    <el-input  v-model.trim="updateCorFactData.legalPersonCode" disabled></el-input>
                </el-form-item>
                <!--工厂描述-->
                <el-form-item :label="nowLangue.farenmingcheng" prop="legalPersonName"  :rules="[{ required: true, message: nowLangue.required}]" >
                    <el-input  v-model.trim="updateCorFactData.legalPersonName" :placeholder="nowLangue.pleaseImport+nowLangue.factoryDescription"></el-input>
                </el-form-item>


                <!--状态-->
                <el-form-item :label="nowLangue.status" class="selectMB10" >
                    <el-radio-group v-model="updateCorFactData.enabled">
                        <!-- 启用 -->
                        <el-radio :label="true" v-cloak>{{nowLangue.enable}}</el-radio>
                        <!-- 禁用 -->
                        <el-radio :label="false" v-cloak>{{nowLangue.forbidden}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <!--保存-->
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="saveUpdateCorFact" v-text="nowLangue.save" :disabled="saveUCFDisabled"></el-button>
                <!--取消-->
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="cancleUpdateCorFact" v-text="nowLangue.cancel"></el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                nowLangue:{},
                // 查询表单
                formInline:{
                    userId:"",
                    userName:"",
                    legalPersonName:"",//法人名称
                    legalPersonCode:"",//法人编码
                    enabled:""
                },
                labelWidth:"80px",
                addCorFactData:{
                    legalPersonName:"",//法人名称
                    legalPersonCode:"",//法人编码
                    // legalPersonId:"",//法人ID
                    userId:"",
                    id:"",
                    userName:"",
                    enabled:true
                },
                updateCorFactData:{
                    id:"",
                    legalPersonName:"",//法人名称
                    legalPersonCode:"",//法人编码
                    // legalPersonId:"",//法人ID
                    userId:"",
                    userName:"",
                    enabled:"",
                    version:""
                },
                legalPersonDropDown:[], //法人名称
                corFactoryData:[],//法人工厂列表
                loading:false,
                disabled:false,
                // radio:1,//默认启用


                //弹窗
                dialogForm1Visible:false,
                dialogForm2Visible:false,
                // 分页
                isPaging:false,
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                totalNumber: 20,
                totalPage:2,
                //查询禁用
                selectDisabled:false,
                saveCFDisabled:false,      //新建弹窗中保存操作按钮禁用
                saveUCFDisabled:false      //修改弹窗中保存操作按钮禁用
            };
        },
        created:function(){
            var _this=this;
            _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
        },
        computed:{
            // powerData(){
            //     return this.$store.state.powerData;
            // },
            languageType(){
                return this.$store.state.languageType;
            }
        },
        watch:{
            languageType(){
                var _this=this;
                _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
            }
        },
        //进入页面立即运行此方法
        mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'CorporateFactory');
            //    //ajax前后查询按钮禁用
            //    $(".selectAll").ajaxStart(function(){
            //        _this.selectDisabled=true;
            //    });
            //    $(".selectAll").ajaxStop(function(){
            //        _this.selectDisabled=false;
            //    });
            _this.legalPersonDropDownValue();//获取法人列表下拉框值
            _this.queryCorFactSheet();    //表单查询
        },
        methods:{
            //法人名称下拉列表值
            legalPersonDropDownValue(){
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"legalPerson/getUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.legalPersonDropDown = data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //查询法人所属工厂明细列表
            queryCorFactSheet(){
                var _this=this;
                _this.loading=true;
                var tmp={
                    userId:_this.formInline.userId,
                    legalPersonCode:_this.formInline.legalPersonCode,
                    legalPersonName:_this.formInline.legalPersonName,
                    enabled:_this.formInline.enabled,
                    pageIndex:_this.pageIndex,
                    pageSize:_this.pageSize
                };
                _this.selectDisabled=true;
                $.ajax({
                    type: 'POST',
                    url: _this.metadata_service+"legalPerson/query",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.loading=false;
                            _this.isPaging=true;
                            _this.totalPage=data.data.totalPage;
                            _this.totalNumber=data.data.totalNumber;
                            _this.corFactoryData=data.data.dataList;
                            // 当前状态的显示
                            for(var i=0;i<_this.corFactoryData.length;i++){
                                if(_this.corFactoryData[i].enabled){
                                    _this.corFactoryData[i].enabled='启用';
                                }else{
                                    _this.corFactoryData[i].enabled='禁用';
                                }
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage)
                        }
                        _this.selectDisabled=false;
                    }
                });
            },
            //新增法人所属工厂信息
            createNew(){
                var _this=this;
                _this.dialogForm1Visible=true;
                if(this.$refs['addCorFactData']) {
                    this.$refs['addCorFactData'].resetFields();
                }
                _this.addCorFactData.legalPersonName = '';
                _this.addCorFactData.legalPersonCode = '';
                _this.addCorFactData.UserId='';
                _this.addCorFactData.userName="";
                _this.addCorFactData.enabled = false;
            },
            //新建保存
            saveCorFact(){
                var _this=this;
                _this.$refs["addCorFactData"].validate((valid) => {
                    if (valid) {
                        var tmp={
                            legalPersonName:_this.addCorFactData.legalPersonName,
                            legalPersonCode:_this.addCorFactData.legalPersonCode,
                            userId:_this.addCorFactData.userId,
                            enabled:_this.addCorFactData.enabled,
                        };
                        _this.saveCFDisabled=true;
                        $.ajax({
                            type: 'POST',
                            url: _this.metadata_service+"legalPerson/add",
                            cache: false,
                            data:JSON.stringify(tmp),
                            dataType:'JSON',
                            contentType: "application/json;charset=utf-8",
                            success: function (data) {
                                if (data.success) {
                                    var success=_this.languageType=="zh"?'保存成功':'Save Success';
                                    _this.common.successTip(success);
                                    _this.dialogForm1Visible=false;
                                    _this.queryCorFactSheet();
                                }else{
                                    _this.common.errorTip(data.errorMessage);
                                }
                                _this.saveCFDisabled=false;
                            }
                        });
                    } else {
                        var err=_this.common.formErrorInfo();
                        _this.common.errorTip(err);
                        return false;
                    }
                });
            },
            //新建取消
            cancleCorFact(){
                var _this=this;
                _this.dialogForm1Visible=false;
                if(this.$refs['addCorFactData']) {
                    this.$refs['addCorFactData'].resetFields();
                }
                _this.addCorFactData.legalPersonName="";
                _this.addCorFactData.legalPersonCode = '';
                _this.addCorFactData.legalPlantCode="";
                _this.addCorFactData.legalPlantDec="";
                _this.addCorFactData.enabled=false;
            },
            // 操作启用禁用
            handleStatus(index, row){
                var _this=this;
                if(row.enabled == '启用'){
                    row.enabled = false;
                }else{
                    row.enabled = true;
                }
                var tmp={
                    id: row.id,
                    legalPlantCode:row.legalPlantCode,
                    legalPersonCode:row.legalPersonCode,
                    enabled: row.enabled}; // legalPlantDec:row.legalPlantDec, 描述不传
                $.ajax({
                    type: 'POST',
                    url: _this.metadata_service+"legalPlant/updatestatus",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            // 当前状态的显示
                            if(row.enabled){
                                row.enabled = "启用";
                                var success=_this.languageType=="zh"?'启用成功':'Enable success';
                                _this.common.successTip(success);
                            }else{
                                row.enabled = "禁用";
                                var success=_this.languageType=="zh"?'禁用成功':'Disable success';
                                _this.common.successTip(success);
                            }
                        }else
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            //点击修改
            updateCorFact(index,row){
                var _this=this;
                _this.dialogForm2Visible=true;
                if(this.$refs['updateCorFactData']) {
                    this.$refs['updateCorFactData'].resetFields();
                }
                _this.updateCorFactData.id = row.id;
                _this.updateCorFactData.userId = row.userId;
                _this.updateCorFactData.userName = row.userName;
                _this.updateCorFactData.legalPersonCode = row.legalPersonCode;
                _this.updateCorFactData.legalPersonName = row.legalPersonName;
                _this.updateCorFactData.enabled = row.enabled;

                if(_this.updateCorFactData.enabled=='禁用'){
                    _this.updateCorFactData.enabled=false;
                }else if(_this.updateCorFactData.enabled=='启用'){
                    _this.updateCorFactData.enabled=true;
                }
            },
            //修改保存
            saveUpdateCorFact(){
                var _this=this;
                _this.$refs["updateCorFactData"].validate((valid) => {
                    if (valid) {
                        var tmp={
                            id:_this.updateCorFactData.id,
                            userId:_this.updateCorFactData.userId,
                            legalPersonCode:_this.updateCorFactData.legalPersonCode,
                            legalPersonName:_this.updateCorFactData.legalPersonName,
                            enabled: _this.updateCorFactData.enabled,
                            version: _this.updateCorFactData.version};
                        _this.saveUCFDisabled=true;
                        $.ajax({
                            type: 'POST',
                            url: _this.metadata_service+"legalPerson/update",
                            cache: false,
                            data:JSON.stringify(tmp),
                            dataType:'JSON',
                            contentType: "application/json;charset=utf-8",
                            success: function (data) {
                                if (data.success) {
                                    var success=_this.languageType=="zh"?'修改成功！':'Modify successfully';
                                    _this.common.successTip(success);
                                    _this.dialogForm2Visible=false;
                                    _this.queryCorFactSheet();
                                }else{
                                    _this.common.errorTip(data.errorMessage);
                                }
                                _this.saveUCFDisabled=false;
                            }
                        });
                    } else {
                        var err=_this.common.formErrorInfo();
                        _this.common.errorTip(err);
                        return false;
                    }
                });

            },
            //修改取消
            cancleUpdateCorFact(){
                var _this=this;
                _this.dialogForm2Visible=false;
            },
            //   分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                var _this=this;
                _this.pageSize=val;
                _this.queryCorFactSheet();

            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                var _this=this;
                _this.pageIndex=val;
                _this.queryCorFactSheet();
            }
        }
    }
</script>



